<template>
  <div>
    <Form ref="form" :rules="formValidate" :label-width="90" :model="form">
          <FormItem label="名称" prop="label">
            <Input v-model="form.label" />
          </FormItem>
          <FormItem label="一级管理员" prop="manager">
            <Select v-model="form.manager" style="width:260px">
              <Option v-for="item in managerList" :value="item.id+''" :key="item.id">{{ item.actualName }}</Option>
            </Select>
          </FormItem>
          <FormItem label="二级管理员" prop="manager2">
            <Select v-model="form.manager2" style="width:260px" clearable>
              <Option v-for="item in managerList" :value="item.id+''" :key="item.id">{{ item.actualName }}</Option>
            </Select>
          </FormItem>
          <FormItem label="三级管理员" prop="manager3" >
            <Select v-model="form.manager3" style="width:260px" clearable>
              <Option v-for="item in managerList" :value="item.id+''" :key="item.id">{{ item.actualName }}</Option>
            </Select>
          </FormItem>
    </Form>
    <Row class="code-row-bg" justify="end" type="flex">
      <Button @click="cancel" style="margin-right:10px">取消</Button>
      <Button @click="save" type="primary">保存</Button>
    </Row>
  </div>
</template>
<script>
  import { workOrderTypeApi } from '@/api/work-order-type';
  import  {employeeApi} from '@/api/employee';
  export default {
    name: 'WorkOrderTypeListForm',
    components: {
    },
    props: {
      //是否为更新表单
      isUpdate: {
        type: Boolean,
        default: true
      },
      //更新的表单数据对象
      updateData: {
        type: Object
      }
    },
    data() {
      return {
        managerList:[],
        //表单数据
        form: {
         //名称
         label:null,
         //等级
         manager:null,
         manager2:null,
         manager3:null,
        },
        //表单验证
        formValidate: {
        //名称
        label:[{ required: true, message: '请输入名称', trigger: 'blur' }],
        //管理员
        manager:[{ required: true, message: '请选一级管理员', trigger: 'blur|change' }],
        }
      };
    },
  watch: {
      updateData: function(newValue, oldValue) {
          this.$refs['form'].resetFields();
          if (this.isUpdate) {
              for (let k in this.form) {
                  this.$set(this.form, k, newValue[k]+'');
              }
              this.$set(this.form, 'id', newValue['id']);
          }
      },
      isUpdate: function(newValue, oldValue) {
          if (!newValue) {
              this.resetForm();
              this.$refs['form'].resetFields();
          }
      }
  },
    created() {},
    mounted() {
      
      (async () => {
        let param = {
          pageNum:1,
          pageSize:100,
          positionId:16
        };
        let res = await employeeApi.getEmployeeList(param);
        this.managerList = res.data.list;
      })();
    },
    methods: {
      cancel() {
        this.$emit('on-form-close');
      },
      save() {
        console.log(this.form)
       this.$refs['form'].validate(valid => {
         if (valid) {
           if (this.isUpdate) {
            this.update();
           } else {
             this.add();
           }
         } else {
          this.$Message.error('参数验证错误，请仔细填写表单数据!');
         }
       });
      },
    resetForm() {
        this.form = {
          //主键
          id:null,
          //名称
          label:null,
          //等级
          form:null,
        };
        this.$refs['form'].resetFields();
      },
      async add() {
        this.$Spin.show();
        let res = await workOrderTypeApi.addWorkOrderType(this.form);
        this.$Message.success(res.msg);
        this.$Spin.hide();
        this.resetForm();
        this.$emit('on-form-close');
      },
      async update() {
        this.$Spin.show();
        let res = await workOrderTypeApi.updateWorkOrderType(this.form);
        this.$Message.success(res.msg);
        this.$Spin.hide();
        this.resetForm();
        this.$emit('on-form-close');
      }
    }
  };
</script>